﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SH.Common.CommonReturn;
using SH.Room.Application.IServices.IRoomType;
using SH.Room.Domain.RoomClass;
using System.Data;
using System.Linq.Expressions;
using System.Reflection;
using static System.Net.Mime.MediaTypeNames;

namespace SH.Room.Read.API.Controllers
{
    /// <summary>
    /// 房型
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoomTypeController : ControllerBase
    {
        /// <summary>
        /// 房型逻辑层
        /// </summary>
        private readonly IRoomTypeServices _roomTypeServices;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="roomTypeServices"></param>
        public RoomTypeController(IRoomTypeServices roomTypeServices)
        {
            _roomTypeServices = roomTypeServices;
        }

        /// <summary>
        /// 测试网关
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {
            return Ok(DateTime.Now);
        }

        /// <summary>
        /// 房型下拉列表显示
        /// </summary>
        /// <returns>受影响行数</returns>
        [HttpGet]
        public async Task<IActionResult> GetRoomList()
        {
            var res = await _roomTypeServices.GetRoomList();
            return Ok(res);
        }


        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="roomTypeAddDto">房型添加</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public async Task<IActionResult> RoomTypeAdd(RoomTypeAddDto roomTypeAddDto)
        {
            var result = await _roomTypeServices.RoomTypeAdd(roomTypeAddDto);
            return Ok(result);
        }

        /// <summary>
        /// 房型显示、查询、分页
        /// </summary>
        /// <param name="rtname">房型名称</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>受影响行数</returns>
        [HttpGet]
        public IActionResult GetRoomTypeList(string? rtname, int pageIndex, int pageSize)
        {
            var res = _roomTypeServices.GetRoomTypeList(rtname, pageIndex, pageSize);
            return Ok(res);
        }

        /// <summary>
        /// 房型删除
        /// </summary>
        /// <param name="rtid">房型ID</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public IActionResult DeleteRoomType(int rtid)
        {
            var info = _roomTypeServices.DeleteRoomType(rtid);

            return Ok(info);
        }

        /// <summary>
        /// 房型信息反填
        /// </summary>
        /// <param name="rtid">房型id</param>
        /// <returns>受影响行数</returns>
        [HttpGet]
        public IActionResult GetFindRoomType(int rtid)
        {
            ApiResult<RoomType> apiResult = new ApiResult<RoomType>();
            var result = _roomTypeServices.GetFindRoomType(rtid);
            apiResult.Success(result);
            return Ok(apiResult);
        }

        /// <summary>
        /// 房型修改
        /// </summary>
        /// <param name="roomTypeUpdDto">房型修改DTO</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public IActionResult UpdRoomType(RoomTypeUpdDto roomTypeUpdDto)
        {
            var res = _roomTypeServices.UpdRoomType(roomTypeUpdDto);
            return Ok(res);

        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="roomType">房型</param>
        /// <returns>受影响行数</returns>
        [HttpPut]
        public IActionResult BatchDelRoomType(List<RoomType> roomType)
        {
            var result = _roomTypeServices.BatchDelRoomType(roomType);
            return Ok(result);
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public IActionResult UpdFile(IFormFile file)
        {
            var saveFile = Directory.GetCurrentDirectory() + @"\wwwroot\pic\" + file.FileName;

            using (FileStream fs = new FileStream(saveFile, FileMode.Create))
            {
                file.CopyTo(fs);
            }
            return Ok(file.FileName);
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="files">文件集合</param>
        /// <returns>受影响行数</returns>
        //[HttpPost]
        //public IActionResult UpdFiles()
        //{
        //    var files = Request.Form.Files;
        //    var saveFile = Directory.GetCurrentDirectory() + @"\wwwroot\pic\";
        //    foreach (var file in files)
        //    {
        //        if (file.Length > 0)
        //        {
        //            var fileName = Path.Combine(saveFile, file.FileName);
        //            using (var stream = new FileStream(fileName, FileMode.Create))
        //            {
        //                file.CopyTo(stream);
        //            }
        //        }
        //    }
        //    return Ok(files.Select(f => f.FileName).ToList());
        //}



    }
}
